package com.situ.mall.common.mapper;

import com.situ.mall.common.entity.Category;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.situ.mall.common.vo.CategoryVO;
import com.situ.mall.common.vo.IndexCategoryVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author JavaWizard
 * @since 2023-09-12
 */
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {

/* 后台请求 */
    //发请求去查分类数据
    @Select("select * from category where status != 1")
    List<Category> all();

    //查询商品表格
    List<CategoryVO> selectByParentId(@Param("name")String name, @Param("parentId")Integer parentId);

    //把一级菜单的id作为二级菜单的parentId 查二级菜单
    @Select("select * from category where parent_id = #{id} and status != 1")
    List<CategoryVO> selectByParentId1(Integer id);

    //新增保存
    @Insert("insert into category(name, parent_id, icon, sequence, status) values (#{name},#{parentId},#{icon},#{sequence},#{status})")
    void save(Category category);

    //更新
    //避免category中有不传值的数据被更新成null，该语句写成动态sql
    void update(Category category);

    //软删除
    @Delete("update category set status = 1 where id = #{id}")
    void delete(Integer id);

    //根据分类名查分类
    Category selectByName(@Param("name")String name, @Param("id")Integer id);

    //编辑——根据ID查询该行数据带到前台
    @Select("select * from category where id = #{id}")
    Category selectById(Integer id);



/*前端请求 */
    //轮播图查一级分类 —> 二级分类
    List<IndexCategoryVO> indexList();

    //传一个一级分类的id，查该分类下最新的八个子分类
    @Select("select * from category where parent_id = #{id} and status = 0 order by sequence desc limit 0,8")
    List<Category> select8(Integer id);
}
